package com.endoc.news.fragment.homefragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.endoc.news.R;
import com.endoc.news.databinding.FragmentHomeBinding;
import com.endoc.news.fragment.base.BaseMvvmFragment;
import com.endoc.news.fragment.base.BaseMvvmViewModel;
import com.endoc.news.netWoke.bean.NewsChannelsBean;
import com.google.android.material.tabs.TabLayout;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseMvvmFragment<FragmentHomeBinding,HomeFragmentViewModel> {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    //private HomeFragmentViewModel mHomeFragmentViewModel;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    //FragmentHomeBinding fragmentHomeBinding;
    /*  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          fragmentHomeBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.fragment_home,container,false);
          return fragmentHomeBinding.getRoot();
      }*/
    HomeFragmentAdapter homeFragmentAdapter;
    NewsChannelsBean newsChannelsBean;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        newsChannelsBean =new NewsChannelsBean();
        homeFragmentAdapter = new HomeFragmentAdapter(getFragmentManager());
        mFragmentDataBinding.vp.setAdapter(homeFragmentAdapter);
        mFragmentDataBinding.tbLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mFragmentDataBinding.tbLayout.setupWithViewPager(mFragmentDataBinding.vp);
        mFragmentDataBinding.vp.setOffscreenPageLimit(1);

        //fragmentHomeBinding.setLifecycleOwner(this);//设置生命周期,DataBinding需调用setLifecycleOwner(LifecycleOwner lifecycleOwner)之后，绑定了LiveData数据源的xml控件才会随着数据变化而改变。

        //mHomeFragmentViewModel = new HomeFragmentViewModel();//此处就已经执行了网络请求,后面才开始注册,可能会有问题
        //getLifecycle().addObserver(mHomeFragmentViewModel);//注册生命周期
        mBaseFragmentViewModel.listMutableLiveData.observe(this, new Observer<List<NewsChannelsBean.ChannelList>>() {
            @Override
            public void onChanged(List<NewsChannelsBean.ChannelList> channelLists) {
                homeFragmentAdapter.setData(channelLists);
            }
        });
       // mBaseFragmentViewModel.getCache();
   /*     NewsChannelsBeanModel newsChannelsBeanModel =new NewsChannelsBeanModel(new IBaseModelListener<NewsChannelsBean>() {

            @Override
            public void success(NewsChannelsBean t, PagingResult... pagingResults) {
                homeFragmentAdapter.setData(t.showapiResBody.channelList);
            }

            @Override
            public void fail(Throwable e) {
                Logger.d("onError==="+e);
            }
        });*/

      /*  try {
            newsChannelsBeanModel.loadNextPage();
        } catch (Exception e) {
            e.printStackTrace();
        }*/

       /* NetClient.getInstance().getApiService(NewsApiInterface.class).getNewsChannels().subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NewsChannelsBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                Logger.d("Disposable==="+d.isDisposed());
            }

            @Override
            public void onNext(NewsChannelsBean newsChannelsBean) {
                Logger.d(newsChannelsBean);
                if(newsChannelsBean.showapiResBody!=null){
                    homeFragmentAdapter.setData(newsChannelsBean.showapiResBody.channelList);
                }
            }

            @Override
            public void onError(Throwable e) {
                Logger.d("onError==="+e);
            }

            @Override
            public void onComplete() {
                Logger.d("onComplete");
            }
        });*/
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public HomeFragmentViewModel createViewModel() {
        //此处也应该使用工厂方式创建,确保唯一单例
      return    new ViewModelProvider(this,new HomeFragmentViewModel.HomeFragmentViewModelFactory()).get(HomeFragmentViewModel.class);
        //return new HomeFragmentViewModel();
    }

}
